<?php
// +----------------------------------------------------------------------
// | BhAdmin [ BhAdmin匠心打造，我们相信，每个伟大的软件都有一个伟大的故事 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020~2023 https://www.bhadmin.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BhAdmin并不是自由软件，未经许可不能去掉BhAdmin相关版权
// +----------------------------------------------------------------------
// | Author: BAIHU  <admin@bhadmin.cn>
// +----------------------------------------------------------------------
namespace app\admin\logic;

use support\Backend\BackendLogic;
use support\Container;
use support\Exception\ApiException;
use support\Service\ConfigDataService;
use support\Service\ConfigService;

class ConfigDataLogic extends BackendLogic
{
    public function __construct()
    {
        $this->service = Container::get(ConfigDataService::class);
    }

    /**
     * 分页列表
     * @return array
     */
    public function getListPage(): array
    {
        $param = request()->all();
        if (empty($param['configId'])) return message();
        $where = [];
        $where[] = ["mark", '=', 1];
        $where[] = ["config_id", '=', (int)$param['configId']];
        if(!empty($param['name'])){
            $where[] = ["title", 'like', "%{$param['name']}%"];
        }
        $list = $this->service->getListPage($where, ['*'], ['sort' => 'asc'], [], $param['limit'] ?? 10);
        return message($list);
    }


    /**
     * 编辑配置
     * @return array
     */
    public function editInfo(): array
    {

        $data = request()->post();
        $id = $data['id'] ?? 0;
        if (empty($data['config_id'])) throw new ApiException('请选择配置分类');
        if (empty($data['title'])) throw new ApiException('请输入配置项标题');
        if (empty($data['type'])) throw new ApiException('请选择配置类型');
        if (empty($data['code'])) throw new ApiException('请输入配置项编码');
        if (empty($data['sort'])) throw new ApiException('请输入排序');
        $save['title'] = trim($data['title']);
        $save['sort'] = $data['sort'];
        $save['config_id'] = $data['config_id'];
        $save['type'] = $data['type'];
        $save['code'] = $data['code'];
        $save['status'] = $data['status'] ?? 1;
        $save['value'] = $data['value'] ?? '';
        $save['options'] = $data['options'] ?? '';
        $save['note'] = $data['note'] ?? '';
        unset($data['id']);
        if ($id == 0) {
            $count = $this->service->getUserCountByWhere([["code", '=', $save['code']], ["mark", "=", 1], ['config_id', '=', $data['config_id']]]);
            if ($count > 0) throw new ApiException('系统中已存在相同的配置编码');
            $this->service->add($save);
        } else {
            $count = $this->service->getUserCountByWhere([["code", '=', $save['code']], ["id", "<>", $id], ["mark", "=", 1], ['config_id', '=', $data['config_id']]]);
            if ($count > 0) throw new ApiException('系统中已存在相同的配置编码');
            $this->service->save($save, $id);
        }
        return message();
    }

}
